import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

// 定义公交线路类
class BusLine {
    String startStation; // 起点站
    String endStation; // 终点站
    String passStations; // 途经站点

    public BusLine(String startStation, String endStation, String passStations) {
        this.startStation = startStation;
        this.endStation = endStation;
        this.passStations = passStations;
    }

    @Override
    public String toString() {
        return "BusLine{" +
                "startStation='" + startStation + '\'' +
                ", endStation='" + endStation + '\'' +
                ", passStations='" + passStations + '\'' +
                '}';
    }
}

// 公交路线查询系统类
public class BusRouteQuerySystem {
    public static void main(String[] args) {
        // 从文件中读取公交线路
        List<BusLine> busLines = readBusLinesFromFile("bus_lines.txt");

        // 输入起点站进行查询
        System.out.println("请输入要查询的起点站：");
        String startStation = inputStartStation();

        // 查询公交线路
        BusLine queryResult = queryBusLine(startStation, busLines);
        if (queryResult != null) {
            System.out.println("查询结果：");
            System.out.println(queryResult);
        } else {
            System.out.println("未找到相关公交线路.");
        }
    }

    // 从文件中读取公交线路
    public static List<BusLine> readBusLinesFromFile(String fileName) {
        List<BusLine> busLines = new ArrayList<>();
        try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
            String line;
            while ((line = br.readLine()) != null) {
                // 按空格分割字符串
                String[] parts = line.split(" ");
                String startStation = parts[0].trim();
                String endStation = parts[1].trim();
                String passStations = parts[2].trim();

                // 创建 BusLine 对象并添加到列表中
                busLines.add(new BusLine(startStation, endStation, passStations));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return busLines;
    }

    // 根据起点站查询公交线路
    public static BusLine queryBusLine(String startStation, List<BusLine> busLines) {
        for (BusLine busLine : busLines) {
            if (busLine.startStation.equals(startStation)) {
                return busLine;
            }
        }
        return null;
    }

    // 输入起点站
    public static String inputStartStation() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要查询的起点站：");
        String startStation = scanner.nextLine();
        scanner.close();
        return startStation;
    }
}
